﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Swan.SwanWakeUp.Controls
{
    /// <summary>
    /// Interaction logic for AlarmClock.xaml
    /// </summary>
    public partial class AlarmClock : UserControl
    {
        private Swan.WakeUpLibrary.Alarm alarm;

        public CustomControlLibrary.AlarmClock CurrentAlarm
        {
            get { return (CustomControlLibrary.AlarmClock)GetValue(AlarmProperty); }
            set { SetValue(AlarmProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Alarm.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AlarmProperty =
            DependencyProperty.Register("CurrentAlarm", typeof(CustomControlLibrary.AlarmClock), typeof(AlarmClock), null);


        public AlarmClock()
        {
            InitializeComponent();
            this.CurrentAlarm = new CustomControlLibrary.AlarmClock();

            this.alarmSetter.DataContext = this.CurrentAlarm;
            this.clock.DataContext = this.CurrentAlarm;
            this.alarm = new WakeUpLibrary.Alarm("\\" + Properties.Settings.Default.AlarmName);
            var st = this.alarm.GetStartTime();
            if (st.HasValue)
            {
                this.CurrentAlarm.StartTime = st.Value;
            }

            this.Loaded += (o, e) =>
                {
                    // Bug must force the alarmsetter to show value
                    this.alarmSetter.udHour.Value = this.CurrentAlarm.StartTime.Hour;
                    this.alarmSetter.udMinute.Value = this.CurrentAlarm.StartTime.Minute;
                };
        }

        private void BtnSetAlarm_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Net.NetworkCredential credential;
                if (Properties.Settings.Default.WakeUpComputer)
                {
                    LoginWindow.GetCredentialsVistaAndUp("SOFTONE", out credential);
                    if (credential != null)
                        alarm.SaveAlarm(this.CurrentAlarm.StartTime, credential.UserName, credential.Password);
                }
                else
                    alarm.SaveAlarm(this.CurrentAlarm.StartTime);
            }
            catch (COMException ex)
            {
                this.HandleComException(ex);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ett fel inträffade: " + ex.Message);
            }
            finally
            {
                var alarmStartTime = alarm.GetStartTime();
                this.CurrentAlarm.StartTime = alarmStartTime  ?? DateTime.Now;

                if (alarmStartTime.HasValue)
                {
                    string day = alarmStartTime.Value.Date == DateTime.Now.Date ? "Today" : alarmStartTime.Value.Date == DateTime.Now.Date.AddDays(1) ? "Tomorrow" : alarmStartTime.Value.Date.ToShortDateString();

                    App.NotifyIcon.ShowBalloonTip("Next alarm", string.Format("Next alarm is set to run {0} at {1}", day, alarmStartTime.Value.ToShortTimeString()), Hardcodet.Wpf.TaskbarNotification.BalloonIcon.Info);
                }
                else
                {
                    App.NotifyIcon.ShowBalloonTip("No alarm", "No alarm is set", Hardcodet.Wpf.TaskbarNotification.BalloonIcon.Warning);
                }
            }
        }

        private void HandleComException(COMException ex)
        {
            string msg = "Ett fel inträffade" + Environment.NewLine;
            switch (ex.ErrorCode)
            {
                case -2147216616: //0x80041318
                    msg = "The task XML contains a value which is incorrectly formatted or out of range.";
                    break;
                case -2147216615: //0x80041319:
                    msg = "The task XML is missing a required element or attribute.";
                    break;
                case -2147023107:
                    msg = "Troligtvis fel användarnamn?";
                    break;
                default:
                    msg = ex.Message;
                    break;
            }

            MessageBox.Show(msg);
        }
    }
}
